package cn.wenhaha.common.core.controller;


import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.wenhaha.common.core.domain.AjaxResult;
import cn.wenhaha.common.utils.ResponseMsg;
import cn.wenhaha.common.utils.ServletUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * web层通用数据处理
 * 
 * @author ruoyi
 */
public class BaseController
{



    /**
     * 获取request
     */
    public HttpServletRequest getRequest()
    {
        return ServletUtils.getRequest();
    }

    /**
     * 获取response
     */
    public HttpServletResponse getResponse()
    {
        return ServletUtils.getResponse();
    }

    /**
     * 获取session
     */
    public HttpSession getSession()
    {
        return getRequest().getSession();
    }



    /**
     * 响应返回结果
     * 
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? success() : error();
    }

    /**
     * 响应返回结果
     * 
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result)
    {
        return result ? success() : error();
    }

    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }



    /**
     * 返回失败消息
     */
    public AjaxResult error()
    {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public Object success(String message)
    {
        return success(message,"");
    }


    /**
     * 返回成功消息
     */
    public Object success(String message,Object data)
    {
        return ResponseMsg.success(getRequest(),message,data);
    }


    /**
     * 返回成功消息
     */
    public Object successToJson(String message,Object data)
    {
        return AjaxResult.success(message,data);
    }

    /**
     * 返回成功消息并跳转
     */
    public Object success(String message,String url)
    {
        AjaxResult success = AjaxResult.success(message);
        success.put(AjaxResult.URL_TAG,url);
        return ResponseMsg.result(getRequest(),success);
    }

    /**
     * 返回失败消息
     */
    public Object error(String message)
    {
        return ResponseMsg.error(getRequest(),message);
    }


    /**
     * 返回失败消息
     */
    public Object error(String message,String url)
    {
        AjaxResult success = AjaxResult.error(message);
        success.put(AjaxResult.URL_TAG,url);
        return ResponseMsg.result(getRequest(),success);
    }

    /**
     * 页面跳转
     */
    public String redirect(String url)
    {
        return StrUtil.format("redirect:{}", url);
    }


    public String templates(String path){
        String ua = getRequest().getHeader("User-Agent");
        boolean mobile = UserAgentUtil.parse(ua).isMobile();
        if (mobile){
            return "default_wap/"+path;
        }else {
            return "default_pc/"+path;
        }
    }


    public void echo(String html){
        try {
            HttpServletResponse response = getResponse();
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = getResponse().getWriter();
            writer.write(html+"<br>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void  jump(String url,Integer time){
        String html="<script>setTimeout(function (){location.href=\"{}\";},{});</script><span>暂停{}秒后继续  >>>  </span><a href=\"/admin1.php/admin/vod/batch.html\" >如果您的浏览器没有自动跳转，请点击这里</a><br>";
        echo(StrUtil.format(html,url,time*1000,time));
    }

    public void refresh(){
        PrintWriter writer=null;
        try {
            writer = getResponse().getWriter();
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IoUtil.close(writer);
        }
    }

}
